package icu.qx.pbs.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.pbs.domain.GbsGood;
import icu.qx.pbs.domain.PbsProuteProcedure;
import icu.qx.pbs.domain.param.ProcedureRouteParam;
import icu.qx.pbs.domain.param.ProcedureRouteUpParam;
import icu.qx.pbs.feign.GbsGoodFeign;
import icu.qx.pbs.mapper.PbsProuteProcedureMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.qx.pbs.mapper.PbsProcedureRouteMapper;
import icu.qx.pbs.domain.PbsProcedureRoute;
import icu.qx.pbs.service.IPbsProcedureRouteService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 工艺路线Service业务层处理
 *
 * @author yong
 * @date 2024-06-15
 */
@Service
public class PbsProcedureRouteServiceImpl implements IPbsProcedureRouteService {
    @Autowired
    private PbsProcedureRouteMapper pbsProcedureRouteMapper;

    @Autowired
    private CodeUtils codeUtils;

    @Autowired
    private PbsProuteProcedureMapper pbsProuteProcedureMapper;

    @Autowired
    private GbsGoodFeign gbsGoodFeign;

    /**
     * 查询工艺路线
     *
     * @param prouteCode 工艺路线主键
     * @return 工艺路线
     */
    @Override
    public PbsProcedureRoute selectPbsProcedureRouteByProuteCode(String prouteCode) {
        return pbsProcedureRouteMapper.selectPbsProcedureRouteByProuteCode(prouteCode);
    }

    @Override
    public PbsProcedureRoute getPbsProcedureRouteNameByProuteCode(String prouteCode) {
        return pbsProcedureRouteMapper.getPbsProcedureRouteNameByProuteCode(prouteCode);
    }

    /**
     * 查询工艺路线列表
     *
     * @param pbsProcedureRoute 工艺路线
     * @return 工艺路线
     */
    @Override
    public List<PbsProcedureRoute> selectPbsProcedureRouteList(PbsProcedureRoute pbsProcedureRoute) {
        return pbsProcedureRouteMapper.selectPbsProcedureRouteList(pbsProcedureRoute);
    }

    /**
     * 新增工艺路线
     *
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int insertPbsProcedureRoute(ProcedureRouteParam procedureRouteParam) {
        try {
            PbsProcedureRoute pbsProcedureRoute = procedureRouteParam.getPbsProcedureRoute();
            String code = codeUtils.generateCode(TableNameEnum.PBS_PROCEDURE_ROUTE.getTableName());
            pbsProcedureRoute.setProuteCode(code);
            pbsProcedureRoute.setCreateBy(SecurityUtils.getUsername());
            pbsProcedureRoute.setCreateTime(DateUtils.getNowDate());
            // 添加工艺路线
            pbsProcedureRouteMapper.insertPbsProcedureRoute(pbsProcedureRoute);
            // 添加工序
            if (procedureRouteParam.getPbsProuteProcedureList() != null && !procedureRouteParam.getPbsProuteProcedureList().isEmpty()) {
                List<PbsProuteProcedure> pbsProuteProcedureList = procedureRouteParam.getPbsProuteProcedureList();
                pbsProuteProcedureList.forEach(e -> {
                    e.setPpProuteCode(code);
                    pbsProuteProcedureMapper.insertPbsProuteProcedure(e);
                });

            }
            // 修改产品
            if (procedureRouteParam.getGbsGoodList() != null && !procedureRouteParam.getGbsGoodList().isEmpty()) {
                List<GbsGood> gbsGoodList = procedureRouteParam.getGbsGoodList();
                gbsGoodList.forEach(e -> e.setPdProuteCode(code));
                gbsGoodFeign.editRoute(gbsGoodList);
            }
            return 1;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    /**
     * 修改工艺路线
     *
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int updatePbsProcedureRoute(ProcedureRouteUpParam procedureRouteUpParam) {
        try {
            PbsProcedureRoute pbsProcedureRoute = procedureRouteUpParam.getPbsProcedureRoute();
            String code = pbsProcedureRoute.getProuteCode();
            pbsProcedureRoute.setUpdateBy(SecurityUtils.getUsername());
            pbsProcedureRoute.setUpdateTime(DateUtils.getNowDate());

            // 删除所有工序
            pbsProuteProcedureMapper.deletePbsProuteProcedureByPpProuteCode(code);
            if (procedureRouteUpParam.getPbsProuteProcedureList() != null && !procedureRouteUpParam.getPbsProuteProcedureList().isEmpty()) {
                // 新增工序
                List<PbsProuteProcedure> pbsProuteProcedureList = procedureRouteUpParam.getPbsProuteProcedureList();
                pbsProuteProcedureList.forEach(e -> {
                    e.setPpProuteCode(code);
                    pbsProuteProcedureMapper.insertPbsProuteProcedure(e);
                });
            }
            if (procedureRouteUpParam.getRemoveGoodList() != null && !procedureRouteUpParam.getRemoveGoodList().isEmpty()) {
                List<GbsGood> removeGoodList = procedureRouteUpParam.getRemoveGoodList();
                // update "0"
                removeGoodList.forEach(e -> e.setPdProuteCode("0"));
                gbsGoodFeign.editRoute(removeGoodList);
            }
            if (procedureRouteUpParam.getGbsGoodList() != null && !procedureRouteUpParam.getGbsGoodList().isEmpty()) {
                List<GbsGood> gbsGoodList = procedureRouteUpParam.getGbsGoodList();
                // update code
                gbsGoodList.forEach(e -> e.setPdProuteCode(code));
                gbsGoodFeign.editRoute(gbsGoodList);
            }
            return pbsProcedureRouteMapper.updatePbsProcedureRoute(pbsProcedureRoute);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    /**
     * 批量删除工艺路线
     *
     * @param prouteCodes 需要删除的工艺路线主键
     * @return 结果
     */
    @Override
    public int deletePbsProcedureRouteByProuteCodes(String[] prouteCodes) {
        return pbsProcedureRouteMapper.deletePbsProcedureRouteByProuteCodes(prouteCodes);
    }

    /**
     * 删除工艺路线信息
     *
     * @param prouteCode 工艺路线主键
     * @return 结果
     */
    @Override
    public int deletePbsProcedureRouteByProuteCode(String prouteCode) {
        return pbsProcedureRouteMapper.deletePbsProcedureRouteByProuteCode(prouteCode);
    }
}
